//
//  YUMainViewController.swift
//  tuniu
//
//  Created by 于世民 on 2019/8/8.
//  Copyright © 2019 于世民. All rights reserved.
//

import UIKit
import Then
import Result
import RxCocoa
import RxSwift
import SnapKit


//MARK -底部动画按钮
class YUBottomAnimatButton: UIView {
    
    var isSelected:Bool {
        get {
            return (titleText.textColor == kThemeTomatoColor ? true : false)
        }
        set {
            if newValue && titleText.textColor != kThemeTomatoColor {
                startAnimation()
            }
            titleText.textColor = newValue ? kThemeTomatoColor : kThemeGreyColor
            imageView.image = newValue ? selectImage : normalImage
            
        }
    }
    
    var animationImages:[UIImage] = []

    var normalImage:UIImage?

    var selectImage:UIImage?

    var titleText:UILabel = UILabel().then {
        $0.textAlignment = .center
        $0.font = UIFont.systemFont(ofSize: 10)
        $0.textColor = kThemeGreyColor
    }
    
    var imageView:UIImageView = UIImageView()
    
    init(title:String?, image:UIImage?, selectImage:UIImage?, frame:CGRect) {
        super.init(frame: frame)
        setupUI()
        titleText.text = title
        imageView.image = image
        self.normalImage = image
        self.selectImage = selectImage
    }
    
    func setupUI() {
        addSubview(titleText)
        addSubview(imageView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleText.snp.makeConstraints({ (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(14)
        })
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(2.5)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(30)
        }
    }
    
    private func startAnimation(){
        imageView.animationImages = animationImages
        imageView.animationDuration = 0.5
        imageView.animationRepeatCount = 1
        imageView.startAnimating()
        
        imageView.image = selectImage
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class YUMainViewController: UITabBarController {
    
    var contentView: UIView?
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        UITabBar.appearance().isTranslucent = false
        UITabBar.appearance().barTintColor = kThemeWhiteColor
        UITabBar.appearance().tintColor = kThemeTomatoColor
        UITabBar.appearance().backgroundImage = UIImage.init()
        UITabBar.appearance().shadowImage = UIImage.init()
        
        contentView = UIView(frame: tabBar.bounds)
        tabBar.addSubview(contentView!)
        // 初始化子控制器
        initSubViewControllers()
    
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        for child in tabBar.subviews {
            if child.isKind(of: UIControl.self) {
                child.removeFromSuperview()
            }
        }
    }
   
}

extension YUMainViewController {
    
    // MARK:- 初始化控制器
    private func initSubViewControllers() {
        
        let classNameArr = ["Home", "Location", "Search", "Journey", "Mine"]
        let moduleNameArr = ["首页", "北京", "发现", "行程", "我的"]
        
        var tabArr: [UIViewController] = []
        let projectName = self.getProjectName()
        
        for i in 0..<classNameArr.count {
            
            let clsName = classNameArr[i]
//            let lowStr = clsName.lowercased()
            //NSClassFormString 需要拼接上项目名字
            let clsType = NSClassFromString(projectName+"YU"+clsName+"ViewController") as! UIViewController.Type
            
            let vc = clsType.init()
            vc.title = moduleNameArr[i]
            
            let button = YUBottomAnimatButton(title: moduleNameArr[i], image: UIImage(named: "rn-more1_big"), selectImage: UIImage(named: "rn-more1_big"), frame: CGRect(x: CGFloat(i)*(kScreenW/CGFloat(classNameArr.count)), y: 0, width: kScreenW/CGFloat(classNameArr.count), height: 49))
    
            for i in 0...10 {
                button.animationImages.append(UIImage(named: "rn-more\(i%5+1)_big")!)
            }
            
            let click = UITapGestureRecognizer()
            
            click.rx.event.asObservable().subscribe(onNext: { (_) in
                for sub in self.contentView!.subviews {
                    if let s = sub as? YUBottomAnimatButton {
                        if s != button {
                            s.isSelected = false
                        }else {
                            s.isSelected = true
                        }
                    }
                }
                self.selectedIndex = i
            }).disposed(by: disposeBag)
            
            button.addGestureRecognizer(click)
            
            if i == 0 {
                button.isSelected = true
            }
            
            contentView?.addSubview(button)
            
            let navVc = YUBaseNavigationController(rootViewController: vc)
            
            tabArr.append(navVc)
        }
     
        self.viewControllers = tabArr
    }
    
    private func getProjectName() -> String {
        guard let infoDict = Bundle.main.infoDictionary else {
            return "."
        }
        let key = kCFBundleExecutableKey as String
        guard let value = infoDict[key] as? String else {
            return "."
        }
        return value + "."
    }
}

